<script setup lang="ts">
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import * as imApi from '@/api/im'

const myModal = ref()

let theTitle = ref<string>('请选择要取消关联的微信账号')
let onlyType = ref<string>('wx')
let paramCustomerId = ref<string>()
const showModal = (id: string, type = 'wx') => {
  selectedRowKey.value = []
  selectedRow.value = []

  onlyType.value = type
  if(onlyType.value == 'wx'){
    theTitle.value = '请选择要取消关联的微信账号'
  }else {
    theTitle.value = '请选择要取消关联的闲鱼账号'
  }

  paramCustomerId.value = id
  myModal.value?.showModal()
  getList(id)
}

const emit = defineEmits<{
  refresh: []
}>()

defineExpose({
  showModal,
})

const dataSource = ref<Array<any>>([])
const columns = [
  {
    title: 'ID',
    key: 'id',
    width: 130,
  },
  {
    title: '头像',
    key: 'avatar',
    width: 130,
  },
  {
    key: 'wxName',
    width: 150,
  },
  {
    key: 'fishId',
    width: 150,
  },
]

interface SearchParams {
  channel: number
  condition?: string | undefined
  content?: string
}
const searchParams = ref<SearchParams>({
  channel: onlyType.value == 'wx' ? 1 : 3, // 1:微信，3：闲鱼
})

// 表格多选
const selectedRowKey = ref<Array<any>>([])
const selectedRow = ref<Array<object>>([])
const onSelectChange = (selectedRowKeys: any[], selectedRows: any[]) => {
  // console.log('selectedRowKey=', selectedRowKeys)
  selectedRowKey.value = selectedRowKeys

  selectedRow.value = selectedRows
  // console.log('selectedRow=', selectedRow.value)
}

const getList = async (customerId: string) => {
  const params = {
    channel: onlyType.value == 'wx' ? 1 : 3, // 1:微信，3：闲鱼,
    customerId,
  }
  try {
    let { state, data, message: msg } = await imApi.getAssociatedRobot(params)
    if (state === 200) {
      if(params.channel === 1){
        dataSource.value = data.wechatRobotList
      } else if(params.channel === 3){
        dataSource.value = data.fishRobotList
      }
    } else {
      message.error(msg)
    }
  } catch (error) {
    message.error('网络请求连接失败~')
  }
}

// 取消关联
const okCancelBind = async () => {
  const params = {
    channel: onlyType.value == 'wx' ? 1 : 3, // 1:微信，3：闲鱼,
    customerId: paramCustomerId.value,
    robotIds: selectedRowKey.value,
  }
  try {
    let { state, message: msg } = await imApi.customerCancelRelevanceRobot(params)
    if (state === 200) {
      message.success('已解除关联')
      emit('refresh')
      myModal.value.cancelModal()
    } else {
      message.error(msg)
    }
  } catch (error) {
    message.error('网络请求连接失败~')
  }
}
</script>

<template>
  <MyModal ref="myModal" :title="theTitle" :top="25" :width="900">
    <template #content>
      <a-table
        style="overflow-x: scroll"
        :columns="columns"
        :data-source="dataSource"
        :pagination="false"
        rowKey="id"
        :row-selection="{ selectedRowKeys: selectedRowKey, onChange: onSelectChange }"
        :scroll="{ y: 300 }"
      >
        <template #emptyText>
          <a-empty description="暂无数据" />
        </template>

        <template #headerCell="{ column }">
          <template v-if="column.key === 'fishId'">
            <span v-if="onlyType == 'wx'">微信ID</span>
            <span v-if="onlyType == 'LdleFish'">闲鱼ID</span>
          </template>
          <template v-if="column.key === 'wxName'">
            <span v-if="onlyType == 'wx'">微信昵称</span>
            <span v-if="onlyType == 'LdleFish'">闲鱼昵称</span>
          </template>
        </template>

        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'id'">
            <span>{{ record.id }}</span>
          </template>

          <template v-else-if="column.key === 'avatar'">
            <a-popover placement="rightTop">
              <template #content>
                <div class="popoverImg">
                  <img :src="record.avatar" alt="" />
                </div>
              </template>
              <div class="imgBox">
                <img :src="record.avatar" alt="获取失败..." />
              </div>
            </a-popover>
          </template>

          <template v-if="column.key === 'wxName'">
            <span>{{ record.nickName }}</span>
          </template>

          <template v-if="column.key === 'fishId'">
            <span v-if="onlyType == 'wx'">{{ record.wechatId }}</span>
            <span v-if="onlyType == 'LdleFish'">{{ record.fishId }}</span>
          </template>
        </template>
      </a-table>

      <div style="text-align: right">
        <a-button @click="myModal.cancelModal()" style="border-radius: 6px;">取消</a-button>
        <a-button @click="okCancelBind" style="margin-left: 10px;border-radius: 6px" type="primary">
          确定取消关联
        </a-button>
      </div>
    </template>
  </MyModal>
</template>

<style scoped lang="less">
.imgBox {
  width: 50px;
  height: 50px;

  img {
    height: 50px;
  }
}

.popoverImg {
  width: 50px;
  img {
    width: 100%;
  }
}
</style>
